#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "dtk_value.h"

DValue* dtk_value_init (DValue* value, DInt type)
{
    if (value == NULL)
    {
        return NULL;
    }

    value->type = type;

    return NULL;
}

DInt8 dtk_value_get_int8(DValue* value)
{
    if (value == NULL || value->type != D_TYPE_INT8)
    {
        return 0;
    }

    return value->data[0].v_int8;
}

DUInt8 dtk_value_get_uint8(DValue* value)
{
    if (value == NULL || value->type != D_TYPE_UINT8)
    {
        return 0;
    }

    return value->data[0].v_uint8;
}

DInt16 dtk_value_get_int16(DValue* value)
{
    if (value == NULL || value->type != D_TYPE_INT16)
    {
        return 0;
    }

    return value->data[0].v_int16;
}

DUInt16 dtk_value_get_uint16(DValue* value)
{
    if (value == NULL || value->type != D_TYPE_UINT16)
    {
        return 0;
    }

    return value->data[0].v_uint16;
}

DInt dtk_value_get_int(DValue* value)
{
    if (value == NULL || value->type != D_TYPE_INT)
    {
        return 0;
    }

    return value->data[0].v_int;
}

DUInt dtk_value_get_uint(DValue* value)
{
    if (value == NULL || value->type != D_TYPE_UINT)
    {
        return 0;
    }

    return value->data[0].v_uint;
}

DLong dtk_value_get_long(DValue* value)
{
    if (value == NULL || value->type != D_TYPE_LONG)
    {
        return 0;
    }

    return value->data[0].v_long;
}

DULong dtk_value_get_ulong(DValue* value)
{
    if (value == NULL || value->type != D_TYPE_ULONG)
    {
        return 0;
    }

    return value->data[0].v_ulong;
}

DInt64 dtk_value_get_int64(DValue* value)
{
    if (value == NULL || value->type != D_TYPE_INT64)
    {
        return 0;
    }

    return value->data[0].v_int64;
}

DUInt64 dtk_value_get_uint64(DValue* value)
{
    if (value == NULL || value->type != D_TYPE_UINT64)
    {
        return 0;
    }

    return value->data[0].v_uint64;
}

DFloat dtk_value_get_float(DValue* value)
{
    if (value == NULL || value->type != D_TYPE_FLOAT)
    {
        return 0;
    }

    return value->data[0].v_float;
}

DDouble dtk_value_get_double(DValue* value)
{
    if (value == NULL || value->type != D_TYPE_DOUBLE)
    {
        return 0;
    }

    return value->data[0].v_double;
}

DPointer dtk_value_get_pointer(DValue* value)
{
    if (value == NULL || value->type != D_TYPE_POINTER)
    {
        return NULL;
    }

    return value->data[0].v_pointer;
}

DString dtk_value_get_string(DValue* value)
{
    if (value == NULL || value->type != D_TYPE_STRING)
    {
        return NULL;
    }

    return value->data[0].v_string;
}

DColor dtk_value_get_color(DValue* value)
{
    DColor color = {0, 0, 0, 0};
    
    if (value == NULL || value->type != D_TYPE_COLOR)
    {
        return color;
    }

    return value->data[0].v_color;
}

void dtk_value_set_int8(DValue* value, DInt8 data)
{
    if (value == NULL)
    {
        return;
    }

    value->type = D_TYPE_INT8;
    value->data[0].v_int8 = data;

    return;
}

void dtk_value_set_uint8(DValue* value, DUInt8 data)
{
    if (value == NULL)
    {
        return;
    }
    
    value->type = D_TYPE_UINT8;
    value->data[0].v_uint8 = data;
    return;    
}

void dtk_value_set_int16(DValue* value, DInt16 data)
{
    if (value == NULL || value->type != D_TYPE_INT16)
    {
        return;
    }

    value->type = D_TYPE_INT16;
    value->data[0].v_int16 = data;
    return;
}

void dtk_value_set_uint16(DValue* value, DUInt16 data)
{
    if (value == NULL)
    {
        return;
    }

    value->type = D_TYPE_UINT16;
    value->data[0].v_uint16 = data;
    return;
}

void dtk_value_set_int(DValue* value, DInt data)
{
    if (value == NULL)
    {
        return;
    }

    value->type == D_TYPE_INT;
    value->data[0].v_int = data;
    return;
}

void dtk_value_set_uint(DValue* value, DUInt data)
{
    if (value == NULL)
    {
        return;
    }
    value->type = D_TYPE_UINT;
    value->data[0].v_uint = data;
    return;
}

void dtk_value_set_long(DValue* value, DLong data)
{
    if (value == NULL)
    {
        return;
    }

    value->type = D_TYPE_LONG;
    value->data[0].v_long = data;
    return;
}

void dtk_value_set_ulong(DValue* value, DULong data)
{
    if (value == NULL)
    {
        return;
    }

    value->type = D_TYPE_ULONG;
    value->data[0].v_ulong = data;
    return;
}

void dtk_value_set_int64(DValue* value, DInt64 data)
{
    if (value == NULL)
    {
        return;
    }

    value->type = D_TYPE_INT64;
    value->data[0].v_int64 = data;
    return;
}

void dtk_value_set_uint64(DValue* value, DInt64 data)
{
    if (value == NULL)
    {
        return;
    }

    value->type = D_TYPE_UINT64;
    value->data[0].v_uint64 = data;
    return;
}

void dtk_value_set_float(DValue* value, DFloat data)
{
    if (value == NULL)
    {
        return;
    }

    value->type = D_TYPE_FLOAT;
    value->data[0].v_float = data;
    return;
}

void dtk_value_set_double(DValue* value, DDouble data)
{
    if (value == NULL)
    {
        return;
    }

    value->type = D_TYPE_DOUBLE;
    value->data[0].v_double = data;
    return;
}

void dtk_value_set_pointer(DValue* value, DPointer data)
{
    if (value == NULL)
    {
        return;
    }

    value->type = D_TYPE_POINTER;
    value->data[0].v_pointer = data;
    return;
}

void dtk_value_set_string(DValue* value, DString data)
{
    if (value == NULL || data == NULL)
    {
        return;
    }

    if (value->data[0].v_string)
    {
        free (value->data[0].v_string);
    }

    value->type = D_TYPE_STRING;
    value->data[0].v_string = strdup(data);
}

void dtk_value_set_color(DValue* value, DColor* color)
{
    if (value == NULL || color == NULL)
    {
        return;
    }

    value->type = D_TYPE_COLOR;
    value->data[0].v_color = *color;
    return;
}